Back to course home
0% completed
Vote For New Content
Microsoft Hard Problems
Regular Expression Matching (hard)
Solution: Regular Expression Matching
First Missing Positive (hard)
Solution: First Missing Positive
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Sliding Window Maximum (hard)
Solution: Sliding Window Maximum
Count Unique Characters of All Substrings of a Given String (hard)
Solution: Count Unique Characters of All Substrings of a Given String
Word Ladder (hard)
Solution: Word Ladder
Word Ladder II (hard)
Solution: Word Ladder II
Number of Flowers in Full Bloom (hard)
Solution: Number of Flowers in Full Bloom
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Score of Parentheses (medium)
Problem Statement
Given a balanced parenthesis string s, return the score of the given string.
Follow the below rules to determine the score of the string s.
"()"has score 1.XYhas scoreX + Y, where X and Y both are balanced parentheses strings.(X)has score2 * X, where X is a balanced parentheses string.
Examples
Example 1:
- Input:
"((()))" - Expected Output: 4
- Justification: The string represents a pair of parentheses inside another pair, which is again inside another pair. This structure results in a score of
((()))=((1*2)*2)=4.
Example 2:
- Input:
"(()())" - Expected Output: 4
- Justification: Here, we have a pair of parentheses inside another pair
(()())and another pair next to it. The total score is(1*2) + (1*2)=4.
Example 3:
- Input:
"(()(()))" - Expected Output: 6
- Justification: The innermost
()scores 1, its surrounding()doubles it to 2, the previous()adjacent to the inner pair scores 1 and is then doubled by its surrounding pair, resulting in2 + (1*2)*2=6.
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Matchsticks to Square
Solution: Score of Parentheses
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself